<?php

/**
 * OrganizationalUnit form.
 *
 * @package    wildlive
 * @subpackage form
 * @author     Maximiliqn Lenkeit
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class OrganizationalUnitForm extends BaseOrganizationalUnitForm
{
  const PARENT_WIDGET_NONE = 0;
  const PARENT_WIDGET_HIDDEN = 1;
  
  const OPTION_PARENT_WIDGET = 'parent_widget';
  const OPTION_PARENT = 'parent';
  const OPTION_ENTITY_ID_READONLY = 'entity_id_readonly';
  
  protected static $embeddedElectiveOfficesKey = 'ElectiveOffices';

  public function configure ()
  {
    $this->useFields(array(
      'name', 'organizational_entity_id'));
    
    $this->widgetSchema->setNameFormat('organizational_unit[%s]');
    
    // Embed form for elective offices
    $embedded_organizational_unit_elective_offices_form = new EmbeddedOrganizationalUnitElectiveOfficesForm($this->getObject());
    $embedded_organizational_unit_elective_offices_form->getWidgetSchema()
      ->setNameFormat('organizational_unit[' . self::$embeddedElectiveOfficesKey . '][%s]');
    $this->embedForm(self::$embeddedElectiveOfficesKey, $embedded_organizational_unit_elective_offices_form);
    
    $this->widgetSchema['organizational_entity_id']->setOption('add_empty', false);
    if ($this->getOption(self::OPTION_ENTITY_ID_READONLY, false) === true)
    {
      //$this->widgetSchema['organizational_entity_id']->setAttribute('disabled', 'disabled');
    }
    
    if ($this->getOption(self::OPTION_PARENT_WIDGET, self::PARENT_WIDGET_NONE) != self::PARENT_WIDGET_NONE)
    {
      $this->widgetSchema['parent_id'] = new WidgetFormOrganizationalUnitCustomEnabledChoice(
          array(
            'label' => 'Übergeordnete Untergliederung'));
      if ($this->getOption(self::OPTION_PARENT) instanceof OrganizationalUnit)
      {
        $this->widgetSchema['parent_id']->setOption(WidgetFormOrganizationalUnitCustomEnabledChoice::OPTION_ENABLED_IDS, 
            array(
              $this->getOption(self::OPTION_PARENT)
              ->getId()));
        $this->setDefault('parent_id', $this->getOption(self::OPTION_PARENT)
          ->getId());
      }
    }
    $this->validatorSchema['parent_id'] = new sfValidatorInteger(); // TODO adjust to meet doctrine
    

    $this->widgetSchema->setLabel('name', 'Name');
    $this->widgetSchema->setLabel('organizational_entity_id', 'Art der Untergliederung');
    $this->widgetSchema->setLabel('parent_id', 'Übergeordnete Untergliederung');
  
  }

  public function bind (array $taintedValues = null, array $taintedFiles = null)
  {
    return parent::bind($taintedValues, $taintedFiles);
  }

  protected function doBind (array $values)
  { 
    $embedded_elective_offices_form_values = $values[self::$embeddedElectiveOfficesKey];
    $this->getElectiveOfficesForm()
      ->bind($embedded_elective_offices_form_values);
      
    return parent::doBind($values);
  }

  public function saveEmbeddedForms ($con = null, $forms = null)
  {
    $this->getElectiveOfficesForm()
      ->save($con);
//    parent::saveEmbeddedForms($con, $forms);
  }

  public function getElectiveOfficesForm ()
  {
    return $this->getEmbeddedForm(self::$embeddedElectiveOfficesKey);
  }

  public function isValid ()
  {
    $embedded_elective_offices_form_validity = $this->getElectiveOfficesForm()->isValid();
    $form_validity = parent::isValid();
    
    return $embedded_elective_offices_form_validity && $form_validity;
  }
  
  protected function updateDefaultsFromObject() {
    parent::updateDefaultsFromObject();
    $this->getElectiveOfficesForm()->updateDefaultsFromObject();
  }
}
